package cn.teduymdj.jxc.controller;


import cn.teduymdj.jxc.myAnnotation.LogAnnotation;
import cn.teduymdj.jxc.pojo.dto.UnitAddNewDTO;
import cn.teduymdj.jxc.pojo.entity.Log;
import cn.teduymdj.jxc.pojo.vo.UnitListItemVO;
import cn.teduymdj.jxc.service.UnitService;
import cn.teduymdj.jxc.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品单位Controller层
 */
@Validated
@RestController
@RequestMapping("/unit")
public class UnitController {

    @Autowired
    private UnitService unitService;

    /**
     * 添加商品单位
     *
     * @param unit 商品单位实体
     * @return
     */
    //日志保存注解,不用管:李柯辰
    @LogAnnotation(content = "添加商品单位", type = Log.INSERT_ACTION)
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult<Void> addUnit(@Valid UnitAddNewDTO unit) {

        unitService.saveUnit(unit);

        return JsonResult.ok();
    }

    @LogAnnotation(content = "启用商品单位", type = Log.UPDATE_ACTION)
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        unitService.setEnable(id);
        return JsonResult.ok();
    }

    @LogAnnotation(content = "禁用商品单位", type = Log.UPDATE_ACTION)
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        unitService.setDisable(id);
        return JsonResult.ok();
    }


    /**
     * 删除商品单位
     *
     * @param id 商品单位ID
     * @return
     */
    @LogAnnotation(content = "删除商品单位", type = Log.DELETE_ACTION)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {

        unitService.delete(id);

        return JsonResult.ok();

    }

    /**
     * 查询所有商品单位
     *
     * @return
     */
    @LogAnnotation(content = "查询所有商品单位", type = Log.SELECT_ACTION)
    @RequestMapping("/list")
    @ResponseBody
    public JsonResult<List<UnitListItemVO>> list() {

        List<UnitListItemVO> unitList = unitService.listAll();

        return JsonResult.ok(unitList);

    }
}
